package com.vt.common.core.util;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class ViolationUnit {
    public static void main(String[] args) {
        String carType = "02";
        String area = "粤";
        String plate = "粤B0R57Y";
        String vin = "LGBF5AE02JR031170";
        String ein = "094729Y";
       System.out.println(getviolation_record());
    }

    /*违章记录
    area:车牌首位
    plate：车牌
    vin：车架号
    ein：发动机号
    * */
    public static String getViolationRecord(String area,String plate,String vin,String ein){

        String carType="02";/*小型车*/
        String urlPost = "http://119.23.239.229/IllegalQuery/user/userApi";
        String account = "YCCLW2017";
        String password = "xSfwVQtSSf4ZrKh6gf";
        String arge ="";
        try {
            vin = vin.substring(vin.length() - 6, vin.length());
            ein = ein.substring(ein.length() - 6, ein.length());
            password = md5(password).toUpperCase();

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date d = new Date();
            String t = df.format(d);
            long epoch = 0;
            try {
                epoch = df.parse(t).getTime() / 1000;
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String DateStamp = String.valueOf(epoch);
            String DataString = account+password+plate+ein+vin+carType+area+DateStamp;
            String sign = md5(DataString);
            arge = "area="+area+"&username="+account+"&carNum="+plate+"&carType="+carType+"&vin="+vin+"&engineNumber="+ein+"&time="+DateStamp+"&sign="+sign;
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
        return httpPost(urlPost,arge);
    }

    public static List<String> getviolation_record(){
        String[] areaArry ={"京","津","沪","渝","冀","豫","云",
                "辽","黑","湘","皖","鲁","新","苏",
                "浙","赣","鄂","桂","甘","晋","蒙",
                "陕","吉","闽","贵","粤","青","藏",
                "川","宁","琼","港","澳","台"};

        String urlPost="http://119.23.239.229/IllegalQuery/user/getAreaSupportApi";
        List<String> list=new ArrayList<String>();
        for(int i=0;i<areaArry.length;i++){
            Map map=new HashMap();
            String area=areaArry[i];
            String arge = "area="+area;
            String json=httpPost(urlPost,arge);
            list.add(json);
        }
        return list;
    }

    private static String httpPost(String urlPost,String arge) {
        try {
            //发送POST请求
            URL url = new URL(urlPost);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            conn.setUseCaches(false);
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Length", "" + arge.length());
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(arge);
            out.flush();
            out.close();
            //获取响应状态
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                System.out.println("connect failed!");
            }
            //获取响应内容体
            String line, result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            in.close();
            return result;
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
        return "no";
    }

    private static String md5(String DateStr) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(DateStr.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            return buf.toString();
        } catch (Exception e) {
            e.printStackTrace();

        }
        return "";
    }
}
